/**
 * @Author:Geln Yang
 * @Created:2008-7-27
 * $ Revision: 1.0 $
 * $ Author: Geln Yang $
 * $ Date: 2008-7-27 $
 */
package org.geln.web.context.impl;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.geln.web.context.ISessionContext;
/**
 * @Author:Geln Yang
 * @Created:2008-7-27
 */
public class SessionContextImpl implements ISessionContext {
	private static final Log logger = LogFactory.getLog(SessionContextImpl.class);
	private static final ThreadLocal<Object> resources = new ThreadLocal<Object>();
	
	@SuppressWarnings("unchecked")
	public void bindResource(Object key, Object value) throws IllegalStateException {
		Map<Object,Object> map = (Map<Object, Object>) resources.get();
		if (map == null) {
			map = new HashMap<Object, Object>();
			resources.set(map);
		}
		if (map.containsKey(key)) {
			throw new IllegalStateException("Already value [" + map.get(key) + "] for key [" + key +
					"] bound to thread [" + Thread.currentThread().getName() + "]");
		}
		map.put(key, value);
		if (logger.isDebugEnabled()) {
			logger.debug("Bound value [" + value + "] for key [" + key + "] to thread [" +
					Thread.currentThread().getName() + "]");
		}

	}

	@SuppressWarnings("unchecked")
	public Object unbindResource(Object key) throws IllegalStateException {
		Map<Object, Object> map = (Map<Object, Object>) resources.get();
		if (map == null || !map.containsKey(key)) {
			throw new IllegalStateException(
					"No value for key [" + key + "] bound to thread [" + Thread.currentThread().getName() + "]");
		}
		Object value = map.remove(key);
		if (map.isEmpty()) {
			resources.set(null);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("Removed value [" + value + "] for key [" + key + "] from thread [" +
					Thread.currentThread().getName() + "]");
		}
		return value;
	}

	@SuppressWarnings("unchecked")
	public Object getResource(Object key) {
		Map<Object, Object> map = (Map<Object, Object>) resources.get();
		if (map == null) {
			return null;
		}
		Object value = map.get(key);
		if (value != null && logger.isDebugEnabled()) {
			logger.debug("Retrieved value [" + value + "] for key [" + key + "] bound to thread [" +
					Thread.currentThread().getName() + "]");
		}
		return value;
	}
	
	public String getHttpSessionId() {
		return (String) getResource(ISessionContext.HTTP_SESSIONID);
	}

	public Locale getLocale() {
		Locale locale = (Locale) getResource(ISessionContext.LOCALE);
		if(locale == null){
			locale = Locale.getDefault();			
		}
		return locale;
	}

	public String getRemoteHost() {
		String remote_host = (String) getResource(ISessionContext.REMOTE_HOST);
		if(remote_host == null)
			remote_host = "unknow host";
		return remote_host;
	}

	public String getAccount() {
		return (String) getResource(ISessionContext.ACCOUNT);
	}
	
	@SuppressWarnings("unchecked")
	public void clearContext(String userId) {
		Map<Object, Object> map = (Map<Object, Object>) resources.get();
        if(map !=null){
            map.clear();
            resources.set(null);
        }
	}

}
